// 云函数入口文件
const axios = require('axios')
const {
    FormData
} = require('formdata-node')

// 云函数入口函数
exports.main = async (event, context) => {
    return request(event)
}

async function request(event) {
    try {
        if (!event.method || event.method === 'get') {
            const res = await axios.get(event.url)
            return res.data
        }
        let data = event.data
        if (event.isFormData && event.data) {
            const formData = new FormData()
            Object.keys(event.data).forEach(key => {
                formData.append(key, event.data[key])
            })
            data = formData
        }
        console.log(event.url, data.get('input'))
        const res = await axios.post(event.url, data)
        return res.data
    } catch (e) {
        console.log(e)
        return {
            e
        }
    }
}